﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using Medianamik.Core;
using Medianamik.Core.Configuration;
using Medianamik.Core.Profile;
using Medianamik.UI.Controls;
using System.Globalization;

namespace Medianamik.UI
{
    public class EditButtonConfigurator
    {
        private const string LOCKED_OR_DISABLED_IMAGE_URL = "/Medianamik/images/Icones/Ico_EditionInactif.gif";
        private const string EDIT_IMAGE_URL = "/Medianamik/images/Icones/Ico_Edition.gif";
        private const string ADD_IMAGE_URL = "/Medianamik/images/Icones/Ico_Plus.gif";
        private static readonly MedianamikConfigSection _config = (MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);

        private Dictionary<Guid, bool> _nodeTypeHasUrl;
        private Dictionary<Guid, bool> NodeTypeHasUrl
        {
            get
            {
                return _nodeTypeHasUrl
                       ?? (_nodeTypeHasUrl = new Dictionary<Guid, bool>());
            }
        }

        private CultureInfo _editionCulture;
        private WebSite _webSite;
        private bool HasWebSite
        {
            get { return _webSite != null; }
        }

        public EditButtonConfigurator(WebSite pListWebsite, CultureInfo pEditionCulture)
        {
            _webSite = pListWebsite;
            _editionCulture = pEditionCulture;
        }

        public void Configure(LockButton pButton, Node pNode)
        {
            pButton.LockedOrDisabledImageUrl = LOCKED_OR_DISABLED_IMAGE_URL;
            pButton.ImageUrl = GetEditImageUrl(pNode);
            pButton.Enabled = EditButtonEnabled(pNode);
            pButton.NodeId = pNode.ID;

            if (_config.EnableMultiLanguageFeatures && 
                ((UserProfile)HttpContext.Current.Profile).EnableMultiLanguageFeatures && 
                !pNode.HasInstanceForCurrentEditionCulture &&
                !pNode.IsLanguageNeutral)
            {
                pButton.NavigateUrl = "";
                pButton.OnClientRequestSucceeded =
                    "radopen('/medianamik/modalwindows/CultureSelectorWindow.aspx?" + DefaultParamNames.NodeID + "=" + pNode.ID + "', 'CultureSelectorWindow');";
            }
        }

        protected string GetEditImageUrl(Node pNode)
        {
            string imageUrl = pNode.HasInstanceForCurrentEditionCulture || pNode.IsLanguageNeutral
                                  ? EDIT_IMAGE_URL
                                  : ADD_IMAGE_URL;
            return imageUrl;
        }

        protected bool EditButtonEnabled(Node pNode)
        {
            //Pour les Noeuds avec une URL, il faut que le Website soit traduit dans la langue en cours
            bool hasUrl = false;
            if (NodeTypeHasUrl.ContainsKey(pNode.NodeTypeID))
            {
                hasUrl = NodeTypeHasUrl[pNode.NodeTypeID];
            }
            else
            {
                var nodeType = NodeTypeManager.GetNodeType(pNode.NodeTypeID);
                hasUrl = nodeType.GetAllProperties().Any(p => string.Equals(p.Name, "Url", StringComparison.OrdinalIgnoreCase));
                NodeTypeHasUrl.Add(pNode.ID, hasUrl);
            }

            if (!HasWebSite) { return true; }

            bool websiteInstanceExists = HasWebSite
                                         && _webSite.Instances.Exists(_editionCulture);
            bool editEnabled = hasUrl
                                   ? websiteInstanceExists ? true : false
                                   : true;

            return editEnabled;
        }
    }
}